package com.scproject.scbef.service.study.hotpolitics.impl;

import com.scproject.dto.ResponseDto;
import com.scproject.scbef.mapper.HotPoliticsMapper;
import com.scproject.scbef.pojo.HotPolitics;
import com.scproject.scbef.pojo.HotPoliticsExample;
import com.scproject.scbef.pojo.vo.ResponseCodeEnum;
import com.scproject.scbef.service.study.hotpolitics.HotPoliticsService;
import com.scproject.utils.JWTUtils;
import com.scproject.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @author LNL
 * @date 2021/5/22 12:37
 */
@Service("hotPoliticsService")
@Transactional
public class HotPoliticsServiceImpl implements HotPoliticsService {

    @Autowired
    private HotPoliticsMapper hotPoliticsMapper;

    @Override
    public ResponseDto getAllHotPolitics() {
        ResponseDto responseDto = null;
        HotPoliticsExample example = new HotPoliticsExample();
        example.setOrderByClause("publish_time desc");
        List<HotPolitics> hotPolitics = hotPoliticsMapper.selectByExampleWithBLOBs(example);
        Optional<List<HotPolitics>> optionalList = Optional.ofNullable(hotPolitics);
        if(optionalList.isPresent()) {
            responseDto = new ResponseDto(ResponseCodeEnum.SUCCESS.toString(),"获取成功",hotPolitics);
        }else {
            responseDto = new ResponseDto(ResponseCodeEnum.ERROR.toString(),"获取失败");
        }
        return responseDto;
    }

    @Override
    public ResponseDto deleteHotPolitics(String id) {
        ResponseDto responseDto = null;
        int isDelete = hotPoliticsMapper.deleteByPrimaryKey(id);
        if(isDelete > 0) {
            responseDto = new ResponseDto(ResponseCodeEnum.SUCCESS.toString(),"删除成功");
        }else {
            responseDto = new ResponseDto(ResponseCodeEnum.ERROR.toString(),"删除失败");
        }
        return responseDto;
    }

    @Override
    public ResponseDto insertHotPolitics(HotPolitics hotPolitics, HttpServletRequest request) {
        ResponseDto responseDto = null;
        //获取当前用户
        Map<String, String> userInfo = JWTUtils.getInfo(request.getHeader("token"));
        hotPolitics.setId(UUIDUtils.getUUID());
        hotPolitics.setCreateUser(userInfo.get("userId"));
        hotPolitics.setCreateTime(new Date());
        int isInsert = hotPoliticsMapper.insertSelective(hotPolitics);
        if(isInsert > 0) {
            responseDto = new ResponseDto(ResponseCodeEnum.SUCCESS.toString(),"添加成功");
        }else {
            responseDto = new ResponseDto(ResponseCodeEnum.ERROR.toString(),"添加失败");
        }
        return responseDto;
    }

    @Override
    public ResponseDto updateHotPolitics(HotPolitics hotPolitics, HttpServletRequest request) {
        ResponseDto responseDto = null;
        hotPolitics.setEditUser(JWTUtils
                .getInfo(request.getHeader("token"))
                .get("userId"));
        hotPolitics.setEditTime(new Date());
        int isUpdate = hotPoliticsMapper.updateByPrimaryKeySelective(hotPolitics);
        if(isUpdate > 0) {
            responseDto = new ResponseDto(ResponseCodeEnum.SUCCESS.toString(),"修改成功");
        }else{
            responseDto = new ResponseDto(ResponseCodeEnum.ERROR.toString(),"修改失败");
        }
        return responseDto;
    }
}
